package GUI;

import javax.swing.JFrame;

import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.Toolkit;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

import common.ChatIF;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class LoginGUI extends JFrame implements ActionListener, KeyListener, ChatIF 
{
	private JPasswordField txtPassword = null;
	private JButton btnLogin;
	private JButton btnExit;
	private MainGUI maingui; 
	
	public LoginGUI(MainGUI maingui) {
		super(); 
		this.maingui= maingui; 
		
		getContentPane().setLayout(null);

		passwordField = new JPasswordField("12345");
		passwordField.setBounds(176, 148, 139, 23);
		getContentPane().add(passwordField);

		textField = new JTextField("Enter user name");
		textField.setBounds(176, 99, 139, 23);
		getContentPane().add(textField);
		textField.setColumns(10);

		JLabel lblUser = new JLabel("User:");
		lblUser.setFont(new Font("Tahoma", Font.ITALIC, 15));
		lblUser.setBounds(73, 99, 72, 23);
		getContentPane().add(lblUser);

		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setFont(new Font("Tahoma", Font.ITALIC, 15));
		lblPassword.setBounds(73, 148, 72, 23);
		getContentPane().add(lblPassword);

		JLabel lblWelcomeToIcm = new JLabel("WELCOME TO ICM");
		lblWelcomeToIcm.setHorizontalAlignment(SwingConstants.CENTER);
		lblWelcomeToIcm.setFont(new Font("Tahoma", Font.BOLD, 30));
		lblWelcomeToIcm.setBounds(0, 0, 434, 40);
		getContentPane().add(lblWelcomeToIcm);
		
		btnLogin = new JButton("Login");
		btnLogin.setBounds(73, 209, 103, 23);
		btnLogin.addActionListener(this);
		getContentPane().add(btnLogin);
		
		btnExit = new JButton("Exit");
		btnExit.setBounds(275, 209, 103, 23);
		btnExit.addActionListener(this);
		getContentPane().add(btnExit);
		
		this.setVisible(true);
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPasswordField passwordField;
	private JTextField textField;

	/**
	 * @param args
	 */
	public int getId() {
		 //try {
			return Integer.parseInt(textField.getText());
	//	} catch (Exception exception) {
		//	common.Perror.pError("Invalid ID number!");
		}
		//return -1;
	//}
	public String getPass() {
		return new String(passwordField.getPassword()); 
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()== btnLogin) {
				 maingui.loginhandler(); 
				 close();
		}
		if (e.getSource()==btnExit){
			System.exit(0);
		}
		
	}


	@Override
	public void keyPressed(KeyEvent e) {
	
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	
	public void display(String message) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * Let us close a window
	 */
	public void close(){ 
		WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
		Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
	}

}
